package de.mrclrchtr.education.account.controller

import de.mrclrchtr.education.account.dto.UserDto
import de.mrclrchtr.education.account.service.UserService
import de.mrclrchtr.education.account.jooq.tables.pojos.User
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/user")
class UserController(@Autowired private val userService: UserService) {

    // 用户更新请求数据对象
    data class UserUpdateRequest(val user: User, val roleIds: List<Int>)


    /**
     * 获取所有用户数据
     */
    @GetMapping("")
    fun getUser(): MutableList<UserDto> {
        return userService.getAllUsers()
    }

    /**
     * 根据id获取用户，并加上角色list
     */
    @GetMapping("/{id}")
    fun getUserById(id: Int): UserDto? {
        return userService.getUserByIdWithRoles(id)
    }

    /**
     * 新增用户
     */
    @PostMapping()
    fun createUser(@RequestBody user: User): String {
        return userService.addUser(user)
    }

    /**
     * 修改用户
     */
    @PutMapping()
    fun updateUser(@RequestBody requestData: UserUpdateRequest): String {
        return userService.updateUser(requestData.user, requestData.roleIds)
    }


    /**
     * 根据id删除用户
     */
    @DeleteMapping("/{id}")
    fun deleteUser(@PathVariable id: Int): String {
        return userService.deleteUserById(id)
    }

}